﻿package com.example.sunjian.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.cyhz_common_component_activity.CCA_Camera_CallBack;
import com.cyhz_common_component_activity.CCA_SystemImgPorter;
import com.util.utilpackage.BitmapUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends Activity implements TestListener{

    private ImageView mImageView;//ceshi 
    private String mParentPath = Environment.getExternalStorageDirectory().getAbsolutePath();

    private int REQUEST_CODE_CAMERA=1;
    private File cameraFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Log.e("onSaveInstanceState","onCreate bundle:"+savedInstanceState);
//        compressBitmap(50);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        float dimen = getResources().getDisplayMetrics().density;
        int dpi = getResources().getDisplayMetrics().densityDpi;

        Log.e("sj","width:"+width+"   height:"+height+"   dimen:"+dimen+"   dpi:"+dpi);

        Toast.makeText(this,"width:"+width+"   height:"+height+"   dimen:"+dimen+"   dpi:"+dpi,Toast.LENGTH_LONG).show();

//       mImageView = (ImageView)findViewById(R.id.img);布局中注释掉了img
        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String imageName = System.currentTimeMillis()+"";
                final String path = mParentPath+"/test"+imageName+".jpg";

                cameraFile = new File(path);

                startActivityForResult(
                        new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)),
                        REQUEST_CODE_CAMERA);







//                CCA_SystemImgPorter.openCamera(MainActivity.this, path
//                        , new CCA_Camera_CallBack() {
//                            @Override
//                            public void back(String s, int i) {
//                                Bitmap bitmap = BitmapFactory.decodeFile(s);
//                                String smallPath = mParentPath+"/test"+imageName+"-small.jpg";
//
//                                try {
//                                    File file = new File(smallPath);
//                                    if(!file.exists()){
//                                        file.createNewFile();
//                                    }
//                                    FileOutputStream fos = new FileOutputStream(file);
//                                    bitmap.compress(Bitmap.CompressFormat.JPEG,10,fos);
//                                    BitmapUtil.compressBitmap(path,smallPath,200,100,100);
//                                    Bitmap smallBitmap = BitmapFactory.decodeFile(smallPath);
//                                    mImageView.setImageBitmap(smallBitmap);
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
//
//
//                            }
//                        });
            }
        });
//        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                findViewById(R.id.tv).postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        Rect rect = new Rect();
//                        img.getWindowVisibleDisplayFrame(rect);
//                        Log.e("sj","post--->"+"rect height:"+rect.height()+"rect left:"+rect.left+"  rect top:"+rect.top+"  rect right:"+rect.right+"  rect bottom:"+rect.bottom);
//                    }
//                }, 2000);
//            }
//        });
//
//        Rect rect = new Rect();
//        img.getWindowVisibleDisplayFrame(rect);
//        Log.e("sj","rect height:"+rect.height()+"  rect left:"+rect.left+"  rect top:"+rect.top+"  rect right:"+rect.right+"  rect bottom:"+rect.bottom);
//        BindListener.getInstance().setListener(this);
//        BindListener.getInstance().print();
//

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE_CAMERA) { // 发送照片
                if (cameraFile != null && cameraFile.exists()) {
                    String path = cameraFile.getAbsolutePath();
                    Log.e("onActivityResult:","intent data :"+data);
                    String smallPath = mParentPath + "/test" + "-small.jpg";
                    BitmapUtil.compressBitmap(path, smallPath, 200, 100, 100);
                    Bitmap smallBitmap = BitmapFactory.decodeFile(smallPath);
                    mImageView.setImageBitmap(smallBitmap);
                }
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e("onSaveInstanceState","bundle:"+outState);
    }

    @Override
    public void print() {
        Log.e("sj","MainActivity print");
    }


    public void compressBitmap(int quality){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.test);
        String savePath = Environment.getExternalStorageDirectory().getPath()+"/test_test";

        try {
            File file = new File(savePath+"/test.jpg");
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,quality,fos);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
